<template>
  <div class="app-container">
    <el-button
      class="filter-item marginRB10"
      icon="el-icon-plus"
      type="primary"
      @click="handleCreate()"
    >添加
    </el-button
    >
    <el-button class="marginRB10" plain type="danger" @click="allDel()"
    >批量删除
    </el-button
    >

    <el-table
      ref="dataList"
      :data="menuList"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      border
      row-key="id"
      style="margin-bottom: 20px"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="id" prop="id"></el-table-column>
      <el-table-column label="名字" prop="name"></el-table-column>
      <el-table-column label="价格" prop="price"></el-table-column>
      <el-table-column label="续费" prop="renew"></el-table-column>
      <el-table-column label="返利" prop="rebate"></el-table-column>
      <el-table-column label="图片" prop="head_picture">
        <template slot-scope="scope">
          <el-popover max-width="220" placement="right" trigger="hover">
            <img
              :src="scope.row.head_picture"
              alt=""
              style="max-width: 200px; max-height: 200px"
            />
            <img
              slot="reference"
              :src="scope.row.head_picture"
              alt=""
              style="width: 50px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="进场人气加成" prop="moods"></el-table-column>
      <el-table-column label="经验加速%" prop="ratio"></el-table-column>
      <el-table-column label="图片" prop="image">
        <template slot-scope="scope">
          <el-popover placement="left" trigger="hover" width="220">
            <img
              :src="scope.row.image"
              alt=""
              style="max-width: 200px; max-height: 200px"
            />
            <img
              slot="reference"
              :src="scope.row.image"
              alt=""
              style="height: 25px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            plain
            size="mini"
            type="primary"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button
          >
          <cus-del-btn @ok="handleDelete(scope.row)"></cus-del-btn>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose"
    >
      <el-form
        ref="dataForm"
        :model="form"
        :rules="rules"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin: 0 auto"
      >
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"/>
        </el-form-item>
        <el-form-item label="续费" prop="renew">
          <el-input v-model="form.renew" placeholder="请输入续费"/>
        </el-form-item>
        <el-form-item label="返利" prop="rebate">
          <el-input v-model="form.rebate" placeholder="请输入返利"/>
        </el-form-item>
        <el-form-item label="爵位图片" prop="head_picture">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleAvatarSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"

          >
            <el-image
              v-if="form.head_picture"
              :src="form.head_picture"
              class="thumbnail"
            ></el-image>
            <el-button v-else type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="进场人气加成" prop="moods">
          <el-input v-model="form.moods" placeholder="请输入进场人气加成"/>
        </el-form-item>
        <el-form-item label="经验加速%" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入经验加速"/>
        </el-form-item>
        <el-form-item label="爵位图片" prop="image">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleAvatarSuccess2"
            :show-file-list="false"
            action=""
            class="avatar-uploader"

          >
            <el-image
              v-if="form.image"
              :src="form.image"
              class="thumbnail"
            ></el-image>
            <el-button v-else type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm">
          确 认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteData, listData, saveData, updateData } from '@/api/title/nobility'

// 图片上传服务器地址

export default {
  data() {
    return {

      confirmBtn: false,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10
      },
      menuList: [],
      dialogFormVisible: false,
      dialogStatus: '',
      titleMap: {
        update: '编辑',
        create: '增加'
      },
      form: {
        id: undefined,
        name: undefined,
        price: undefined,
        renew: undefined,
        rebate: undefined,
        head_picture: undefined,
        moods: undefined,
        ratio: undefined,
        image: undefined
      },
      rules: {
        name: [{ required: true, message: '请输入名字', trigger: 'blur' }],
        price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
        renew: [{ required: true, message: '请输入续费', trigger: 'blur' }],
        rebate: [{ required: true, message: '请输入返利', trigger: 'blur' }],
        head_picture: [
          { required: true, message: '请输入头像框图片', trigger: 'blur' }
        ],
        moods: [
          { required: true, message: '请输入进场人气加成', trigger: 'blur' }
        ],
        ratio: [{ required: true, message: '请输入经验加速', trigger: 'blur' }],
        image: [{ required: true, message: '请输入爵位图片', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleAvatarSuccess() {
      this.form.head_picture = this.ossLastImgUrl
    },

    handleAvatarSuccess2() {
      this.form.image = this.ossLastImgUrl
    },
    getList() {
      this.listLoading = true
      listData().then(response => {
        this.menuList = response.data
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    handleCreate(row) {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.form = []
      this.form.id = row.id
      this.form.name = row.name
      this.form.price = row.price
      this.form.renew = row.renew
      this.form.rebate = row.rebate
      this.form.head_picture = row.head_picture
      this.form.moods = row.moods
      this.form.ratio = row.ratio
      this.form.image = row.image

      this.dialogStatus = 'update'
      this.dialogFormVisible = true

      this.form = Object.assign({}, this.form)
    },
    handleDelete(row) {
      if (this.confirmBtn) return
      this.confirmBtn = true
      let data = { ids: row.id }
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    allDel() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      let delNum = []
      this.$refs.dataList.selection.forEach(k => {
        delNum.push(k.id)
      })
      if (delNum.length === 0) {
        return
      }
      let data = { ids: delNum.toString() }
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        } else {
          this.confirmBtn = false
        }
      })
    },
    submitForm() {
      if (this.confirmBtn) return
      this.$refs['dataForm'].validate(valid => {
        if (valid && this.dialogStatus === 'create') {
          this.confirmBtn = true
          let formData = new FormData()
          for (let item in this.form) {
            this.form[item] === undefined
              ? ''
              : formData.append(item, this.form[item])
          }
          saveData(formData).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
              this.dialogFormVisible = false
            } else {
              this.confirmBtn = false
            }
          })
        } else if (valid && this.dialogStatus === 'update') {
          this.confirmBtn = true
          let formData = new FormData()
          for (let item in this.form) {
            this.form[item] === undefined
              ? ''
              : formData.append(item, this.form[item])
          }
          updateData(formData).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
              this.dialogFormVisible = false
            } else {
              this.confirmBtn = false
            }
          })
        }
      })
    },
    resetForm() {
      this.form = {
        id: undefined,
        name: undefined,
        price: undefined,
        renew: undefined,
        rebate: undefined,
        head_picture: undefined,
        moods: undefined,
        ratio: undefined,
        image: undefined
      }
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    }
  }
}
</script>

<style>
.thumbnail {
  width  : 150px;
  height : 150px;
}
</style>
